package com.martin.dms.dataservices.hib;

import java.util.List;

import org.hibernate.Session;

import com.martin.dms.dataservices.DeliveryDataService;
import com.martin.dms.model.Delivery;
import com.martin.dms.model.hib.DeliveryHib;

/**
 * Hibernate implementation of the Delivery data service.
 * 
 * @author mtsvetkov
 */
public class DeliveryDataServiceHib implements DeliveryDataService {

    private static final String DELIVERY_GET_ALL_QUERY = "Delivery.getAll";

    private Session session;

    /**
     * Constructor that expects a hibernate session.
     * 
     * @param session - the current hibernate session.
     */
    public DeliveryDataServiceHib(Session session) {
        this.session = session;
    }

    /**
     * {@inheritDoc}
     */
    public Delivery create(Delivery dmsObject) {
        DeliveryHib delivery = new DeliveryHib();
        delivery.setSupplier(dmsObject.getSupplier());
        delivery.setItem(dmsObject.getItem());
        delivery.setAmount(dmsObject.getAmount());
        delivery.setDate(dmsObject.getDate());
        session.save(delivery);
        return delivery;
    }

    /**
     * {@inheritDoc}
     */
    public void delete(long id) {
        DeliveryHib delivery = get(id);
        session.delete(delivery);
    }

    /**
     * {@inheritDoc}
     */
    public DeliveryHib get(long id) {
        return (DeliveryHib) session.get(DeliveryHib.class, Long.valueOf(id));
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public List<Delivery> getAll() {
        return session.getNamedQuery(DELIVERY_GET_ALL_QUERY).list();
    }

}
